class MinRequest {
	// 默认配置
	config = {
		isLoading: true,
		baseURL: '',
		header: {
			'content-type': 'application/json',
			'accept': 'application/json'
		},
		method: 'GET',
		dataType: 'json',
		responseType: 'text'
	}

	// 拦截器
	interceptors = {
		request: (func) => {
			if (func) {
				MinRequest.requestBefore = func
			} else {
				MinRequest.requestBefore = (request) => request
			}
		},
		response: (func) => {
			if (func) {
				MinRequest.requestAfter = func
			} else {
				MinRequest.requestAfter = (response) => response
			}
		}
	}

	static requestBefore(config) {
		return config
	}

	static requestAfter(response) {
		return response
	}

	// 判断url是否完整
	static isCompleteURL(url) {
		return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
	}

	// 设置配置
	setConfig(func) {
		this.config = func(this.config)
	}
	// 发起请求
	request(options = {}) {
		options.isLoading = options.isLoading !== undefined ? options.isLoading : this.config.isLoading
		
		
		let mid = uni.getStorageSync('mid')
		options.data = {
			...options.data,
			mid:mid
		}
		
		if (options.isLoading) {
			if(options.data.isLoading!==0){
				uni.showLoading({
					title: '加载中...'
				})
			}
		}
		
		options.baseURL = options.baseURL || this.config.baseURL
		options.dataType = options.dataType || this.config.dataType
		options.url = MinRequest.isCompleteURL(options.url) ? options.url : (options.baseURL + options.url)
		// options.data = options.data
		
		let authorization = uni.getStorageSync('token');
		if(authorization){
			authorization = authorization+'_'+mid
			options.header = {
				...options.header,
				...this.config.header,
				authorization
			}
		}else{
			options.header = {
				...options.header,
				...this.config.header
			}
		}
		
		options.method = options.method || this.config.method
		options = {
			...options,
			...MinRequest.requestBefore(options)
		}
		return new Promise((resolve, reject) => {
			options.success = function(res) {
				resolve(MinRequest.requestAfter(res))
			}
			options.fail = function(err) {
				reject(MinRequest.requestAfter(err))
			}
			options.complete = function() {
				// uni.hideLoading()
			}
			
			uni.request(options)
		})
	}

	get(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'GET'
		return this.request(options)
	}

	post(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.request(options)
	}
	postNotToken(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		return this.requestNotToken(options)
	}
	put(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'PUT'
		return this.request(options)
	}

	patch(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'POST'
		options.patch = true
		return this.request(options)
	}

	delete(url, data, options = {}) {
		options.url = url
		options.data = data
		options.method = 'DELETE'
		return this.request(options)
	}
}

export default MinRequest
